/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import cs4280.bean.*;
import cs4280.dao.*;
import cs4280.action.*;
import javax.servlet.http.HttpSession;

/**
 *
 * @author PangPei
 */
public class ticketBooking extends HttpServlet {

    public void getSeating(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         Screening screening = new Screening();
        screening = (Screening)request.getAttribute("screening");

        int movieid = screening.getMovieid();
        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        movie = movieDAO.getMovie(movieid);
        request.setAttribute("movie", movie);

        HouseDAO houseDAO = new HouseDAO();
        House house = new House();
        int houseID = screening.getHouseid();
        house = houseDAO.getHouse(houseID);
        request.setAttribute("house", house); //set the house of the movie

        ArrayList<Seating> seatings = new ArrayList<Seating>();
        String seatingString = screening.getSeating();
        int row = house.getRownumber();
        int col = house.getColnumber();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                Seating seating = new Seating();
                seating.setRow(i);
                seating.setCol(j);
                seating.setId(i * col + j);
                seating.setStatus(Integer.parseInt(String.valueOf(seatingString.charAt(i * col + j))));
                seatings.add(seating);
            }
        }
        request.setAttribute("seatings", seatings);

    }

    public void bookTicket(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ScreeningDAO screeningDAO = new ScreeningDAO();
        Screening screening = new Screening();
        int screeningID = Integer.parseInt(request.getParameter("screening-time"));

        screening = screeningDAO.getScreening(screeningID);
        request.setAttribute("screening", screening); //set which screening is going to book

        this.getSeating(request, response);

    }

    public void reserveTicket(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ScreeningDAO screeningDAO = new ScreeningDAO();
        Screening screening = new Screening();
        int screeningID = Integer.parseInt(request.getParameter("screeningid"));

        screening = screeningDAO.getScreening(screeningID);
        request.setAttribute("screening", screening); //set which screening is going to book

        this.getSeating(request, response);
    }

       public void releaseTicket(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ScreeningDAO screeningDAO = new ScreeningDAO();
        Screening screening = new Screening();
        int screeningID = Integer.parseInt(request.getParameter("screeningid"));

        screening = screeningDAO.getScreening(screeningID);
        request.setAttribute("screening", screening); //set which screening is going to book

        this.getSeating(request, response);
    }

     public void doReserveTicket(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ArrayList<Ticket> tickets=new ArrayList<Ticket>();

        Screening screening = new Screening();
        ScreeningDAO screeningDAO = new ScreeningDAO();
        screening = screeningDAO.getScreening(Integer.parseInt(request.getParameter("screeningid")));
        String seatingS = screening.getSeating();

        String seatBook = request.getParameter("tickets");
        int number = Integer.parseInt(request.getParameter("number"));

        String seatsReserve[] = seatBook.split(",");

        String temp1;
        String temp2;
        int seatNo;
        int movieid=screening.getMovieid();
        int screeningid=screening.getId();

        char a='2';

        for (int i = 0; i < number; i++) {

            seatNo = Integer.parseInt(seatsReserve[i]);
            Ticket ticket=new Ticket();
            ticket.setSeatingNo(seatNo);
            ticket.setScreeningid(screeningid);
            tickets.add(ticket);
            if (seatNo == 0) {
                seatingS = a + seatingS.substring(1);
            } else if (seatNo == seatingS.length()-1 ) {
                seatingS = seatingS.substring(0, seatNo) + a;
            } else {
                temp1 = seatingS.substring(0, seatNo);
                temp2 = seatingS.substring(seatNo + 1,seatingS.length());
                seatingS = temp1 + a + temp2;
            }
        }

        switch (screeningDAO.setSeating(screeningid, seatingS)) {
            case OK:{
                request.setAttribute("msg", "Tickets are reserved successfully");
                HttpSession session=request.getSession();
                session.setAttribute("ticketsReserve", tickets);
                break;}
            case FAIL:
                request.setAttribute("msg", "Sorry something failed, please try agian");
                break;
        }

        screening = screeningDAO.getScreening(screeningid);
        request.setAttribute("screening", screening); //set which screening is going to book

        this.getSeating(request, response);

    }

     public void doReleaseTicket(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ArrayList<Ticket> tickets=new ArrayList<Ticket>();

        Screening screening = new Screening();
        ScreeningDAO screeningDAO = new ScreeningDAO();
        screening = screeningDAO.getScreening(Integer.parseInt(request.getParameter("screeningid")));
        String seatingS = screening.getSeating();

        String seatBook = request.getParameter("tickets");
        int number = Integer.parseInt(request.getParameter("number"));

        String seats[] = seatBook.split(",");
        String temp1;
        String temp2;
        int seatNo;
        int movieid=screening.getMovieid();
        int screeningid=screening.getId();

        char a='0';

        for (int i = 0; i < number; i++) {

            seatNo = Integer.parseInt(seats[i]);
            if (seatNo == 0) {
                seatingS = a + seatingS.substring(1);
            } else if (seatNo == seatingS.length()-1 ) {
                seatingS = seatingS.substring(0, seatNo) + a;
            } else {
                temp1 = seatingS.substring(0, seatNo);
                temp2 = seatingS.substring(seatNo + 1,seatingS.length());
                seatingS = temp1 + a + temp2;
            }
        }

        switch (screeningDAO.setSeating(screeningid, seatingS)) {
            case OK:
                request.setAttribute("msg", "Tickets are released successfully");
                break;
            case FAIL:
                request.setAttribute("msg", "Sorry something failed, please try agian");
                break;
        }

        screening = screeningDAO.getScreening(screeningid);
        request.setAttribute("screening", screening); //set which screening is going to book

        this.getSeating(request, response);

    }


    public void doPreBookTicket(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session=request.getSession();

        Member member=(Member) session.getAttribute("member");
        ArrayList<Ticket> tickets=new ArrayList<Ticket>();

        Screening screening = new Screening();
        ScreeningDAO screeningDAO = new ScreeningDAO();
        screening = screeningDAO.getScreening(Integer.parseInt(request.getParameter("screeningid")));
        String seatingS = screening.getSeating();
        session.setAttribute("screening", screening); //set which screening is going to book

        String seatBook = request.getParameter("tickets");

        int number = Integer.parseInt(request.getParameter("number"));

        String seats[] = seatBook.split(",");
        String temp1;
        String temp2;
        int seatNo;
        int movieid=screening.getMovieid();
        int screeningid=screening.getId();
        int pricePT=screening.getPrice();
        
        char a='1';

        if(member!=null&&member.getRole().equalsIgnoreCase("manager"))
            a='2';

        for (int i = 0; i < number; i++) {
            Ticket ticket=new Ticket();
            ticket.setMovieid(movieid);
            ticket.setScreeningid(screeningid);
            ticket.setPrice(pricePT);
            seatNo = Integer.parseInt(seats[i]);
            ticket.setSeatingNo(seatNo);
            if (seatNo == 0) {
                seatingS = a + seatingS.substring(1);
            } else if (seatNo == seatingS.length()-1 ) {
                seatingS = seatingS.substring(0, seatNo) + a;
            } else {
                temp1 = seatingS.substring(0, seatNo);
                temp2 = seatingS.substring(seatNo + 1,seatingS.length());
                seatingS = temp1 + a + temp2;
            }
            if (a!='2')
                tickets.add(ticket);
        }
        session.setAttribute("tickets", tickets);
        session.setAttribute("seatingS", seatingS);
        
        Price price=new Price();
        price.setPrice(pricePT);
        price.setTicketNumber(number);
        session.setAttribute("price", price);

    }



    public void doBookTicket(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session=request.getSession();
        Member member=(Member)session.getAttribute("member");
        Price price=(Price)session.getAttribute("price");
        Screening screening = new Screening();
        ScreeningDAO screeningDAO = new ScreeningDAO();

        screening = (Screening)session.getAttribute("screening");
        String seatingS = (String) session.getAttribute("seatingS");

        int memberid=0;
        if(member!=null)
            memberid=member.getId();
        
        int payment=0;
        if (Integer.parseInt(request.getParameter("paymentMethod"))==1)
            payment=price.getPrice();
        if(member!=null&&member.getRole().equalsIgnoreCase("Customer"))
        {
           loyaltyControll loyaltyC=new loyaltyControll();
           if(payment==0)
               loyaltyC.minusLoyaltyPoints(memberid, price.getTotalPriceLP());
           else
               loyaltyC.addLoyaltyPoints(memberid, price.getTotalPrice());
        }
        MemberDAO memberDAO=new MemberDAO();
        //update user session
        member=memberDAO.getMember(memberid);
        session.setAttribute("member", member);

        ArrayList<Ticket> tickets=(ArrayList<Ticket>)session.getAttribute("tickets");
        BookingDAO bookingDAO=new BookingDAO();
        bookingDAO.addBookings(tickets, memberid, payment);

        String msg = "";
        int screeningid = screening.getId();
        switch (screeningDAO.setSeating(screeningid, seatingS)) {
            case OK:
                request.setAttribute("msg", "The booking is done successfully");
                break;
            case FAIL:
                request.setAttribute("msg", "Sorry something failed, please try agian");
                break;
        }

        screening = screeningDAO.getScreening(screeningid);
        request.setAttribute("screening", screening); //set which screening is going to book

        this.getSeating(request, response);
        
        session.removeAttribute("seatingS");
        session.removeAttribute("screening");
        session.removeAttribute("price");
        session.removeAttribute("tickets");

    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
}
